import datetime
import re
import time
import time
import cantools
import os
import ldfparser

year = datetime.datetime.now().year
HEADER_FMT = '''\
/**
 * The MIT License (MIT)
 *
 * Copyright (c) {year} Liu bingqian 
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/**
 * This file was generated by cantools version {version} {date}.
 */

#ifndef ldf.h
#define ldf.h

#ifdef __cplusplus
extern "C" {{
#endif

/* Frame ids. */
{frame_id_defines}

/* Frame lengths in bytes. */
{frame_length_defines}

/* Extended or standard frame types. */
{is_extended_frame_defines}

/* Frame cycle times in milliseconds. */
{frame_cycle_time_defines}

/* Signal choices. */
{choices_defines}

{structs}
{declarations}

#ifdef __cplusplus
}}
#endif

#endif
'''


def _generate_frame_id_defines(database_name, frames):
    return '\n'.join([
        '#define {}_{}_FRAME_ID (0x{:02x}u)'.format(
            database_name,
            frame.name,
            frame.frame_id)
        for frame in frames
    ])


def _generate_frame_length_defines(database_name, frames):
    result = '\n'.join([
        '#define {}_{}_LENGTH ({}u)'.format(
            database_name,
            frame.name,
            frame.length)
        for frame in frames
    ])
    return result


def _generate_frame_length_defines(database_name, frames):
    result = '\n'.join([
        '#define {}_{}_LENGTH ({}u)'.format(
            database_name,
            frame.name,
            frame.length)
        for frame in frames
    ])
    return result


def ldf_generate(database,
                 is_master=True,
                 database_name='exp',
                 # header_name,
                 # source_name,
                 # fuzzer_source_name,
                 # floating_point_numbers=True,
                 # bit_fields=False,
                 # use_float=False,
                 # node_name=None,
                 tx_padding=0xff):
    date = time.ctime()
    print(_generate_frame_id_defines(database_name, database.frames))
    print(_generate_frame_length_defines(database_name, database.frames))
    # print(_generate_frame_length_defines(database_name,
    #                                                       database.signals,
    #                                                       node_name))
    # signals = database.keys()
    # print(signals)
    # header = database['header']
    # print(header)
    # nodes = database['nodes']
    # print(nodes)
    # signals = database['signals']
    # print(signals[0])
    # diagnostic_signals = database['diagnostic_signals']
    # print(diagnostic_signals)
    # frames = database['frames']
    # print(frames)
    # diagnostic_frames = database['diagnostic_frames']
    # print(diagnostic_frames)
    # node_attributes = database['node_attributes']
    # print(node_attributes)
    # schedule_tables = database['schedule_tables']
    # print(schedule_tables)
    # signal_encoding_types = database['signal_encoding_types']
    # print(signal_encoding_types)
    # signal_representations = database['signal_representations']
    # print(signal_representations)
